package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.reflect.TypeToken;
import com.stark.photomovie.render.GLTextureView;
import com.stark.pmu.IPhotoMovieView;
import com.stark.pmu.PhotoMovieFragment;
import com.stark.pmu.PhotoMoviePresenter;
import com.stark.video.player.VideoPlayerActivity;
import com.stark.video.player.VideoPlayerEntry;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.ActivityPicConvertBinding;
import flc.ast.fragment.PicFilterFragment;
import flc.ast.fragment.PicMusicFragment;
import flc.ast.fragment.PicTurnFragment;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.TimeUtil;

public class PicConvertActivity extends BaseAc<ActivityPicConvertBinding> implements IPhotoMovieView {

    public static List<String> listPath=new ArrayList<>();
    public static PhotoMoviePresenter mMoviePresenter;
    private PicFilterFragment picFilterFragment;
    private PicTurnFragment picTurnFragment;
    private PicMusicFragment picMusicFragment;


    @Override
    protected int onCreate() {
        return R.layout.activity_pic_convert;
    }

    @Override
    protected void initView() {
//        R.layout.fragment_pmu_photo_movie
        mDataBinding.ivPicConvertBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //设置音乐相册
        if (mMoviePresenter == null) {
            mMoviePresenter = new PhotoMoviePresenter(mContext, this);
        }
        mMoviePresenter.attachView(this);
        mMoviePresenter.setPhotos(listPath);
        mDataBinding.tlPic.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                if (tab.getText().toString().equals(getString(R.string.pic_convert_text1))){
                    hideFragment(fragmentTransaction);
                    if (picFilterFragment==null){
                        picFilterFragment=new PicFilterFragment();
                        fragmentTransaction.add(R.id.flPicConvert, picFilterFragment);
                    }
                    fragmentTransaction.show(picFilterFragment);
                    fragmentTransaction.commit();
                }else if (tab.getText().toString().equals(getString(R.string.pic_convert_text2))){
                    hideFragment(fragmentTransaction);
                    if (picTurnFragment==null){
                        picTurnFragment=new PicTurnFragment();
                        fragmentTransaction.add(R.id.flPicConvert, picTurnFragment);
                    }
                    fragmentTransaction.show(picTurnFragment);
                    fragmentTransaction.commit();
                }else if (tab.getText().toString().equals(getString(R.string.pic_convert_text3))){
                    hideFragment(fragmentTransaction);
                    if (picMusicFragment==null){
                        picMusicFragment=new PicMusicFragment();
                        fragmentTransaction.add(R.id.flPicConvert, picMusicFragment);
                    }
                    fragmentTransaction.show(picMusicFragment);
                    fragmentTransaction.commit();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mDataBinding.tlPic.addTab(mDataBinding.tlPic.newTab().setText(R.string.pic_convert_text1));
        mDataBinding.tlPic.addTab(mDataBinding.tlPic.newTab().setText(R.string.pic_convert_text2));
        mDataBinding.tlPic.addTab(mDataBinding.tlPic.newTab().setText(R.string.pic_convert_text3));
        mDataBinding.tvPicConvertDerive.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvPicConvertDerive:
                save();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void save(){
        mMoviePresenter.saveVideo(new PhotoMoviePresenter.ISaveCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onProgress(int percent) {
                showDialog(getString(R.string.save_video_ing)+percent+getString(R.string.unit_percent));
            }

            @Override
            public void onEnd(boolean success, String filePath) {
                dismissDialog();
                if (success){
                    FileP2pUtil.copyPrivateVideoToPublic(mContext, filePath);//保存到手机相册
                    ToastUtils.showShort(getString(R.string.save_to_album));
                    saveRecord(filePath);
                }else{
                    ToastUtils.showShort(getString(R.string.save_failure));
                }
            }
        });
    }

    //保存记录
    private void saveRecord(String videoPath) {
        List<RecordBean> listSave = new ArrayList<>();
        String time = TimeUtils.getNowString(new SimpleDateFormat("yyyy/MM/dd"));
        listSave.add(new RecordBean(FileUtils.getFileName(videoPath),
                TimeUtil.getMmss(MediaUtil.getDuration(videoPath)), FileUtils.getSize(videoPath),
                time, videoPath));
        List<RecordBean> listSp = SPUtil.getObject(mContext, new TypeToken<List<RecordBean>>() {
        }.getType());
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            SPUtil.putObject(mContext, listSp, new TypeToken<List<RecordBean>>() {
            }.getType());
        } else {
            SPUtil.putObject(mContext, listSave, new TypeToken<List<RecordBean>>() {
            }.getType());
        }
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (picFilterFragment != null) {
            transaction.hide(picFilterFragment);
        }
        if (picTurnFragment != null) {
            transaction.hide(picTurnFragment);
        }
        if (picMusicFragment != null) {
            transaction.hide(picMusicFragment);
        }
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @NonNull
    @Override
    public GLTextureView getGLTextureView() {
        return mDataBinding.glTextureView;
    }

    @Override
    public void onPreparingPhoto(float progress) {
        showDialog(getString(R.string.load_img_ing));
    }

    @Override
    public void onPreparedPhoto(int prepared, int total) {
        dismissDialog();
    }

    @Override
    public void onPreparedError() {
        dismissDialog();
    }
}